<?php

/**
 * SHOP 商户
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/
define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');

if($_REQUEST['act']=='list'){
	$r_id=isset($_REQUEST['r_id'])?$_REQUEST['r_id']:0;
	$keywords=isset($_REQUEST['keywords'])?$_REQUEST['keywords']:0;
	$sql="select * from ".ly()."shop a ,".ly()."shop_region b";
	$where=" where a.r_id=b.r_id and a.del_status='0'";
	if($r_id){
		$where.=" and a.r_id='{$r_id}'";
	}
	if($keywords){
		$where.=" and a.shop_name like '%$keywords%'";
	}
	$orderby=" order by a.s_id desc";
	$sql.=$where.$orderby;
	$arr=array();
	$arr['r_id']=$r_id;
	$arr['keywords']=$keywords;
	$smarty->assign('sou',$arr);
	/*分页开始*/
	$num='5';
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
	$res=$db->getAll($sql);
	$sql="select r_id,r_name from ".ly()."shop_region where r_del='0'";
	$quyus=$db->getAll($sql);
	$smarty->assign('quyu_list',$quyus);
	$smarty->assign('shop',$res);
	$smarty->display('user_sh_list.html');
}elseif($_REQUEST['act']=='edit'){
	$sql1="select r_id,r_name from ".ly()."shop_region where r_del='0' and  r_zt='0'";
	$row=$db->getAll($sql1);
	$s_id=$_REQUEST['s_id'];
	if($s_id>'0'){
		$sql="select * from ".ly()."shop where s_id='{$s_id}'";
		$res=$db->getRow($sql);
		$smarty->assign('edit',$res);
	}
	$smarty->assign('quyu',$row);
	$smarty->display('user_sh_edit.html');
}elseif($_REQUEST['act']=='insert'){
	$r_id = empty($_POST['r_id']) ? 0 : $_POST['r_id'];
    $shop_name = empty($_POST['shop_name']) ? 0 : ($_POST['shop_name']);
    $password = empty($_POST['shop_password']) ? 123456 : ($_POST['shop_password']);
    $shop_password=md5($password);
    $shop_zt= empty($_POST['shop_zt']) ? 0 : ($_POST['shop_zt']);
    $shop_email = empty($_POST['shop_email']) ? 0 : ($_POST['shop_email']);
    $shop_tel = empty($_POST['shop_tel']) ? 0 : ($_POST['shop_tel']);
    $shop_img = empty($_POST['shop_img']) ? $img : ($_POST['shop_img']);
	$shop_imgs=$_FILES['shop_img']['error'];
    $start_time=$_POST['start_time'];
    $shop_address=$_POST['shop_address'];
    $start_price=$_POST['start_price'];
    $shop_tj=$_POST['shop_tj'];
    $s_id=$_POST['s_id'];
	if($shop_imgs==0){
		require("../includes/upload_admin.php");//加载图片上传类
		$up=new upphoto;
		$up->get_ph_tmpname($_FILES['shop_img']['tmp_name']);
		$up->get_ph_type($_FILES['shop_img']['type']);
		$up->get_ph_size($_FILES['shop_img']['size']);
		$up->get_ph_name($_FILES['shop_img']['name']);
		$up->save();
		$shop_img=$up->ph_name;
	}
	if($s_id>'0'){
		$sql="update ".ly()."shop set r_id='{$r_id}',shop_password='{$shop_password}',shop_zt='{$shop_zt}',shop_email='{$shop_email}',shop_tel='{$shop_tel}',shop_img='{$shop_img}',start_time='{$start_time}',shop_address='{$shop_address}',start_price='{$start_price}',shop_tj='{$shop_tj}'where s_id='{$s_id}'";
		$res=$db->query($sql);
	}else{
		$sql="insert into ".ly()."shop (r_id,shop_name,shop_password,shop_zt,shop_email,shop_tel,shop_datetime,shop_img,start_time,shop_address,start_price,shop_tj) " . "VALUES('" . $r_id . "','" . $shop_name . "','" . $shop_password . "','" . $shop_zt . "','" . $shop_email . "','" . $shop_tel . "','" . date('Y-m-d H:i:s') . "','" . $shop_img . "','" . $start_time . "','" . $shop_address . "','" . $start_price . "','" . $shop_tj . "') ";
    	$res=$db->query($sql);
	}
    if($res=='1'){
    	$href[]=array('text'=>"返回配送列表",'href'=>'user_sh.php?act=list');
         sys_msg("操作成功！",$href);
     	}else{
     		$href[]=array('text'=>"重新编辑",'href'=>'user_sh.php?act=edit');
         sys_msg("操作失败！",$href);
    }
}elseif($_REQUEST['act']=='del'){
	$s_id=$_REQUEST['s_id'];
	$sql="update ".ly()."shop set del_status='1' where s_id='{$s_id}'";
	$res=$db->query($sql);
	if($res){
		$href[]=array('text'=>"返回配送列表",'href'=>'user_sh.php?act=list');
         sys_msg("操作成功！",$href);
     	}else{
     		$href[]=array('text'=>"返回配送列表",'href'=>'user_sh.php?act=edit');
         sys_msg("操作失败！",$href);
	}

}elseif($_REQUEST['act']=='disable'){
	$s_id=isset($_POST['id'])?$_POST['id']:0;
	$vals=isset($_POST['val'])?$_POST['val']:0;
	if($vals==1){$zt='0';}else{$zt='1';}
	$sql="update ".ly()."shop set shop_zt='{$zt}' where s_id='{$s_id}'";
	$res=$db->query($sql);
	if($res){
		echo "1";	
	}else{
		echo "2";
	}
}elseif($_REQUEST['act']=='name'){
	$shop_name=$_REQUEST['shop_name'];
	$r_id =$_REQUEST['r_id'];
	$sql="select r_id from ".ly()."shop where shop_name='{$shop_name}' and r_id='{$r_id}' and del_status='0'";
	$res=$db->getOne($sql);
	if($res){
		echo '1';
	}else{
		echo '2';
	}
}
?>